package com.drug.wms.service.impl;

import com.drug.common.enums.BusinessSeqEnum;
import com.drug.common.utils.DateUtils;
import com.drug.common.utils.SecurityUtils;
import com.drug.common.utils.uuid.IdUtils;
import com.drug.wms.domain.BusFile;
import com.drug.wms.domain.BusGoodsStockDetail;
import com.drug.wms.domain.BusGoodsStockInfo;
import com.drug.wms.domain.BusSkuStock;
import com.drug.wms.mapper.BusFileMapper;
import com.drug.wms.mapper.BusGoodsStockDetailMapper;
import com.drug.wms.mapper.BusGoodsStockInfoMapper;
import com.drug.wms.page.BusGoodsStockInfoPage;
import com.drug.wms.service.GenerateSequence;
import com.drug.wms.service.IBusGoodsStockInfoService;
import com.drug.wms.service.IBusSkuStockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 商品入库清单Service业务层处理
 *
 * @author ruoyi
 * @date 2021-12-01
 */
@Service
public class BusGoodsStockInfoServiceImpl implements IBusGoodsStockInfoService {
    @Autowired
    private BusGoodsStockInfoMapper busGoodsStockInfoMapper;
    @Autowired
    private BusGoodsStockDetailMapper busGoodsStockDetailMapper;
    @Autowired
    private BusFileMapper busFileMapper;

    /**
     * 查询商品入库清单
     *
     * @param id 商品入库清单主键
     * @return 商品入库清单
     */
    @Override
    public BusGoodsStockInfo selectBusGoodsStockInfoById(String id) {
        return busGoodsStockInfoMapper.selectBusGoodsStockInfoById(id);
    }

    /**
     * 查询商品入库清单列表
     *
     * @param busGoodsStockInfo 商品入库清单
     * @return 商品入库清单
     */
    @Override
    public List<BusGoodsStockInfo> selectBusGoodsStockInfoList(BusGoodsStockInfo busGoodsStockInfo) {
        return busGoodsStockInfoMapper.selectBusGoodsStockInfoList(busGoodsStockInfo);
    }
    /**
     * 查询商品入库清单列表(小程序使用)
     *
     * @param busGoodsStockInfo 商品入库清单
     * @return 商品入库清单
     */
    @Override
    public List<BusGoodsStockInfo> selectBusGoodsStockInfoListByOr(BusGoodsStockInfo busGoodsStockInfo) {
        return busGoodsStockInfoMapper.selectBusGoodsStockInfoListByOr(busGoodsStockInfo);
    }
    /**
     * 新增商品入库清单
     *
     * @param BusGoodsStockInfoPage 商品入库清单
     * @return 结果
     */
    @Override
    @Transactional
    public String insertBusGoodsStockInfo(BusGoodsStockInfoPage busGoodsStockInfoPage) {
        //插入入库单据
        BusGoodsStockInfo busGoodsStockInfo = busGoodsStockInfoPage.getBusGoodsStockInfo();
        String seqNo = GenerateSequence.genSeqNo(BusinessSeqEnum.RK.getCode());
        busGoodsStockInfo.setDocNo(seqNo);
        String mid = IdUtils.fastUUID();
        busGoodsStockInfo.setEntryUserId(SecurityUtils.getUserId());
        busGoodsStockInfo.setEntryDeptId(SecurityUtils.getDeptId());
        busGoodsStockInfo.setEntryDate(new Date());
        busGoodsStockInfo.setInputStatus(0L);//草稿
        busGoodsStockInfo.setId(mid);
        busGoodsStockInfoMapper.insertBusGoodsStockInfo(busGoodsStockInfo);

        //插入附件表
        List<BusFile> files = busGoodsStockInfoPage.getFiles();
        if (files != null && files.size() != 0) {
            for (BusFile b : files) {
                b.setBusId(mid);
                busFileMapper.insertBusFile(b);
            }
        }
        return mid;
    }

    @Override
    public int insertBusGoodsStockInfo2(BusGoodsStockInfo busGoodsStockInfo) {
        busGoodsStockInfo.setCreateTime(DateUtils.getNowDate());
        String seqNo = GenerateSequence.genSeqNo(BusinessSeqEnum.RK.getCode());
        busGoodsStockInfo.setDocNo(seqNo);
        busGoodsStockInfo.setEntryUserId(SecurityUtils.getUserId());
        busGoodsStockInfo.setEntryDeptId(SecurityUtils.getDeptId());
        busGoodsStockInfo.setEntryDate(new Date());
        busGoodsStockInfo.setInputStatus(0L);//草稿
        return busGoodsStockInfoMapper.insertBusGoodsStockInfo(busGoodsStockInfo);
    }

    /**
     * 修改商品入库清单
     *
     * @param busGoodsStockInfo 商品入库清单
     * @return 结果
     */
    @Override
    public int updateBusGoodsStockInfo(BusGoodsStockInfo busGoodsStockInfo) {
        return busGoodsStockInfoMapper.updateBusGoodsStockInfo(busGoodsStockInfo);
    }

    /**
     * 修改商品入库详情
     *
     * @param busGoodsStockInfoPage
     * @return
     */
    @Override
    @Transactional
    public String updateBusGoodsStockInfo(BusGoodsStockInfoPage busGoodsStockInfoPage) {
        BusGoodsStockInfo busGoodsStockInfo = busGoodsStockInfoPage.getBusGoodsStockInfo();
        busGoodsStockInfoMapper.updateBusGoodsStockInfo(busGoodsStockInfo);

        busFileMapper.deleteBusFileByBusId(busGoodsStockInfo.getId());
        //插入附件表
        List<BusFile> files = busGoodsStockInfoPage.getFiles();
        if (files != null && files.size() != 0) {
            for (BusFile b : files) {
                b.setBusId(busGoodsStockInfo.getId());
                busFileMapper.insertBusFile(b);
            }
        }
        return busGoodsStockInfo.getId();
    }

    @Autowired
    private IBusSkuStockService iBusSkuStockService;

    @Override
    @Transactional
    public int submitBusGoodsStockInfo(BusGoodsStockInfo busGoodsStockInfo) {
        int i = busGoodsStockInfoMapper.updateBusGoodsStockInfo(busGoodsStockInfo);
        if (1 == i) {
            //获取入库单信息
            busGoodsStockInfo = busGoodsStockInfoMapper.selectBusGoodsStockInfoById(busGoodsStockInfo.getId());
            //查询当前入库详情，转化成入库单
            BusGoodsStockDetail busGoodsStockDetail = new BusGoodsStockDetail();
            busGoodsStockDetail.setStockId(busGoodsStockInfo.getId());
            List<BusGoodsStockDetail> busGoodsStockDetails = busGoodsStockDetailMapper.selectBusGoodsStockDetailList(busGoodsStockDetail);
            List<BusSkuStock> busSkuStockList = new ArrayList<>();
            for (BusGoodsStockDetail b : busGoodsStockDetails) {
                BusSkuStock busSkuStock = new BusSkuStock();
                busSkuStock.setSkuId(b.getGoodsNo());
                busSkuStock.setGoodsName(b.getGoodsName());
                busSkuStock.setUnit(b.getUnitNo());
                busSkuStock.setBaseUnit(b.getBasicUnitNo());
                busSkuStock.setBatchNo(b.getBatchNo());
                busSkuStock.setGoodsStatus(0l);
                busSkuStock.setAmount(new BigDecimal(b.getGoodsNum()));
                busSkuStock.setCanSaleAmount(new BigDecimal(b.getGoodsNum()));
                busSkuStock.setGoodsPosition(b.getReceivingGoodsNo());
                busSkuStock.setAverageCostPrice(b.getGoodsPrice());
                busSkuStock.setQualityStatus(0l);
                busSkuStock.setManufacturerNo(busGoodsStockInfo.getSupplyCompany());
                busSkuStock.setWarehouseNo(busGoodsStockInfo.getReceivingWarehouse());
                busSkuStock.setProduceDate(b.getProductionDate());
                busSkuStock.setCanSaleAmount(new BigDecimal(b.getGoodsNum()));
                busSkuStock.setExpirationDate(b.getExpireDate());
                busSkuStockList.add(busSkuStock);
            }
            //入库操作
            iBusSkuStockService.insertOrUpdateSkuStock(busSkuStockList);
        }
        return 1;
    }

    /**
     * 批量删除商品入库清单
     *
     * @param ids 需要删除的商品入库清单主键
     * @return 结果
     */
    @Override
    public int deleteBusGoodsStockInfoByIds(String[] ids) {
        return busGoodsStockInfoMapper.deleteBusGoodsStockInfoByIds(ids);
    }

    /**
     * 删除商品入库清单信息
     *
     * @param id 商品入库清单主键
     * @return 结果
     */
    @Override
    public int deleteBusGoodsStockInfoById(String id) {
        return busGoodsStockInfoMapper.deleteBusGoodsStockInfoById(id);
    }
}
